﻿using Microsoft.AspNetCore.Http;
using System;
using System.Text.RegularExpressions;

namespace Canteen.Core.Common
{
    public class Getting : IGetting
    {
        private readonly HttpContext _httpContext;
        public Getting(IHttpContextAccessor httpContextAccessor)
        {
            _httpContext = httpContextAccessor?.HttpContext;
        }
        public RequestDevice Device
        {
            get
            {
                RequestDevice device = new RequestDevice
                {
                    IpAddress = _httpContext.Connection.RemoteIpAddress.ToString(),//获取远程IP地址
                    UserAgent = _httpContext.Request.Headers["User-Agent"]
                };

                string ug = device.UserAgent.ToLower();
                if (Regex.IsMatch(ug, "nt 10.0") || Regex.IsMatch(ug, "nt 6.4")) device.Os = "Windows 10";
                else if (Regex.IsMatch(ug, "nt 6.3")) device.Os = "Windows 8.1";
                else if (Regex.IsMatch(ug, "nt 6.2")) device.Os = "Windows 8";
                else if (Regex.IsMatch(ug, "nt 6.1")) device.Os = "Windows 7";
                else if (Regex.IsMatch(ug, "nt 6.0")) device.Os = "Windows Vista/Server 2008";
                else if (Regex.IsMatch(ug, "nt 5.2")) device.Os = "Windows Server 2003";
                else if (Regex.IsMatch(ug, "nt 5.1")) device.Os = "Windows xp";
                else if (Regex.IsMatch(ug, "nt 5")) device.Os = "Windows 2008";
                else if (Regex.IsMatch(ug, "iphone")) device.Os = "iPhone";
                else if (Regex.IsMatch(ug, "ipod")) device.Os = "iPod";
                else if (Regex.IsMatch(ug, "ipad")) device.Os = "iPad";
                else if (Regex.IsMatch(ug, "ios")) device.Os = "Ios";
                else if (Regex.IsMatch(ug, "mac")) device.Os = "Mac";
                else if (Regex.IsMatch(ug, "android")) device.Os = "Android";
                else if (Regex.IsMatch(ug, "sunos")) device.Os = "SunOS";
                else if (Regex.IsMatch(ug, "unix")) device.Os = "Unix";
                else if (Regex.IsMatch(ug, "linux")) device.Os = "Linux";
                else device.Os = "Other";

                return device;
            }
        }

        /// <summary>
        ///  根据当前系统时间加随机序列生成订单号  20位
        /// </summary>
        public string OrderNo
        {
            get
            {
                return $"{DateTime.Now:yyMMddHHmmssfff}{new Random().Next(10000, 99999)}";//yyyy-MM-dd HH:mm:ss
            }
        }
    }
}